# Jukee.py - Jukee
#
# Copyright (C) 2008 Ruben Medellin <ruben.medellin.c@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the
#    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#    Boston, MA  02111-1307  USA

"""
Main entry point. This file contains the script that will initialize the system,
either the server or the client.
"""


import sys
import time
from jukee.server.player import Player
from jukee.client.streamer import Streamer
from jukee.server.jukebox import *
from jukee.client.listener import *
from jukee.server.remoting import startServices
from jukee.server.gui.jukeeServerWindow import JukeeServerWindow
from jukee.client.clientgui import JukeeClientWindow
from jukee.server import servermain
from jukee.client import clientmain

import gtk  ## XXX

def main():

  if len(sys.argv) < 2:
    print "An action is required"
    return

  ##### FIXME
  try:
    gtk.main_quit()
  except:
    warn("Gtk main_quit called just for debugging purposes")
    pass

  action = sys.argv[1]

  if action == 'simpleserver':
    Player(host = "localhost", port = 8080).play()

  elif action == 'simpleclient':    
    Streamer(host = "localhost", port = 8080, fileloc="/home/chubas/Desktop/V.mp3").play()

  elif action == 'server':

    # NOTE: Signal also caught in Pyro threads. Exceptions is not *easily* catchable =(
    if len(sys.argv) >= 3 and sys.argv[2] == 'no-gui':
      name_server, event_server, jukee_server = startServices()
      while 1:
        answer = raw_input("Press start to refresh pipeline. Type QUIT to quit\n")
        if answer == '':
          jukee_server.jukebox.player.refresh()
        elif answer == 'QUIT':
          return
    else:
      servermain.main()
      

  elif action == 'client':

    if len(sys.argv) >= 3 and sys.argv[2] == 'no-gui':
      listener = EventListener()
      listener.start()
      while 1:
        time.sleep(1)
    else:
      clientmain.main()
  else:
    print "Invalid action"

if __name__ == '__main__':
  main()
